/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.engine.property;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;

import com.sinodata.bsm.center.dao.PropertyValueDao;
import com.sinodata.bsm.center.dao.impl.PropertyValueDaoImpl;
import com.sinodata.bsm.center.util.spring.SpringContextHolder;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-14 下午2:56:50          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class PropertyValueObjectPersistentThread extends Thread {
    private final static Logger logger = Logger.getLogger(PropertyValueObjectPersistentThread.class);
    private static PropertyValueObjectPersistentThread instance = new PropertyValueObjectPersistentThread();
    private LinkedBlockingQueue<Object[]> queue = new LinkedBlockingQueue<Object[]>();
    private PropertyValueDao dao = SpringContextHolder.getBean(PropertyValueDaoImpl.class);

    /**
     * 
     */
    private PropertyValueObjectPersistentThread() {
        this.start();
    }

    public static PropertyValueObjectPersistentThread getInstance() {
        return instance;
    }

    public void add(Object[] value) {
        queue.add(value);
    }

    public void run() {
        while (true) {
            Long timeMillis = System.currentTimeMillis();
            List<Object[]> list = new ArrayList<Object[]>();
            queue.drainTo(list);
            if (list.size() > 0) {
                try {
                    dao.insertObjectValues(list);
                } catch (Exception e) {
                    logger.error(e);
                }
            }
            if (System.currentTimeMillis() - timeMillis < 60000L) {
                try {
                    Thread.sleep(60000L - (System.currentTimeMillis() - timeMillis));
                } catch (Exception e) {
                }
            }
        }
    }

}
